package com.coin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.AdminBank;
import com.coin.dto.AdminBankDto;
import com.coin.mapper.AdminBankMapper;
import com.coin.service.AdminBankService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther: 李 力
 * @Date: 2025/6/6
 * @Description: com.coin.service.impl
 * @version: 1.0
 */
@Service
public class AdminBankServiceImpl extends ServiceImpl<AdminBankMapper, AdminBank> implements AdminBankService {

    /**
     * @Description: 分页查询
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: bankCard  page
     * @return: R
     */
    @Override
    public Page<AdminBank> findByPage(Page<AdminBank> page, String bankCard) {
        LambdaQueryWrapper<AdminBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.like(StrUtil.isNotEmpty(bankCard), AdminBank::getBankCard, bankCard);
        return page(page, queryWrapper);
    }

    /**
     * @Description: 新增一个银行卡
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: adminBank
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBank(AdminBank adminBank) {
        if (adminBank == null) {
            throw new RuntimeException("参数为空");
        }
        return save(adminBank);
    }

    /**
     * @Description: 修改一个银行卡
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param: adminBank
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBank(AdminBank adminBank) {
        if (adminBank == null) {
            throw new RuntimeException("修改失败");
        }
        return updateById(adminBank);
    }

    /**
     * @Description: 修改银行卡的状态
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param: bankId  status
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long bankId, Byte status) {
        if (bankId == null || status == null) {
            throw new RuntimeException("参数为空");
        }
        AdminBank adminBank = new AdminBank();
        adminBank.setId(bankId);
        adminBank.setStatus(status);
        return updateById(adminBank);
    }

    @Override
    public List<AdminBankDto> getAllAdminBanks() {
        LambdaQueryWrapper<AdminBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(AdminBank::getStatus, 1);
        List<AdminBank> list = list(queryWrapper);
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }
        List<AdminBankDto> collect = list.stream().map(s -> {
            AdminBankDto t = new AdminBankDto();
            BeanUtil.copyProperties(s, t);
            return t;
        }).collect(Collectors.toList());
        return collect;
    }
}
